package com.dingding.special.user.userinfo.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.beans.special.Specialuserinfo;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.special.user.userinfo.dao.ISpecialUserInfoDao;
import com.dingding.special.user.userinfo.dao.sql.GetUserInfoSQL;
import com.dingding.special.user.userinfo.dto.ResGetUserInfoDTO;
import com.dingding.special.user.userinfo.dto.ResUserDatumDTO;

/**
 * 
 * @ClassName: SpecialUserInfoDaoImpl 
 * @Description: 专车用户信息DAO实现类 
 * @author YHQ
 * @date 2016年8月18日 下午4:11:38
 * @since V2.4.0
 * 
 */
@Component("specialUserInfoDao")
public class SpecialUserInfoDaoImpl implements ISpecialUserInfoDao {

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource(name="SQLBaseDao")
	private ISQLBaseDao sqlBaseDao;
	
	@Resource(name="criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;
	
	/**
	 * 
	 * @Title: getUserInfo 
	 * @Description: 获取用户信息 
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return ResGetUserInfoDTO    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public ResGetUserInfoDTO getUserInfo(String userId) {
		
		try {
			// 获取非专车用户详情
			List<ResGetUserInfoDTO> resGetUserInfoDTOs = sqlBaseDao.listSqlToCustomBean(ResGetUserInfoDTO.class, GetUserInfoSQL.GET_USER_INFO, userId);
			
			if(resGetUserInfoDTOs != null && resGetUserInfoDTOs.size() > 0){
				return resGetUserInfoDTOs.get(0);
			}else{
				
				// 获取专车用户详情
				List<ResGetUserInfoDTO> userInfo = sqlBaseDao.listSqlToCustomBean(ResGetUserInfoDTO.class, GetUserInfoSQL.GET_SPECIAL_USER_INFO, userId);
				
				return userInfo.get(0);
			}
		} catch (Exception e) {
			log.error("获取用户信息时出现异常   类："+this.getClass()+" getUserInfo()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getSpecialUserInfo 
	 * @Description: 获取用户详情
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return boolean    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public Specialuserinfo getSpecialUserInfo(String userId) {
		
		Criterion criterion = Restrictions.eq("userId", userId);
		
		try {
			return criteriaBaseDao.queryCriteriaListUnique(Specialuserinfo.class, criterion);
		} catch (Exception e) {
			log.error("获取用户详情时出现异常   类："+this.getClass()+" getSpecialUserInfo()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getSpecialUserLogin 
	 * @Description: 获取用户登录信息 
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return Specialuserlogin    返回类型
	 * 
	 */
	@Override
	public Specialuserlogin getSpecialUserLogin(String userId) {
		
		try {
			
			return criteriaBaseDao.getById(userId, Specialuserlogin.class);
		} catch (Exception e) {
			log.error("根据用户Id获取用户登录信息时出现异常   类：" + this.getClass()+" getSpecialUserLogin()", e);
		}
		
		return null;
	}

	
	/**
	 * 
	 * @Title: geUserDatum 
	 * @Description: 获取用户资料
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return ResUserDatumDTO    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public ResUserDatumDTO getUserDatum(String userId) {
		
		try {
			
			List<ResUserDatumDTO> userDatumDTOs = sqlBaseDao.listSqlToCustomBean(ResUserDatumDTO.class, GetUserInfoSQL.GET_USER_DATUM, userId);
			
			if(userDatumDTOs != null && userDatumDTOs.size() > 0){
				return userDatumDTOs.get(0);
			}

		} catch (Exception e) {
			log.error("获取用户资料时出现异常   类: "+this.getClass()+" geUserDatum()", e);
		}
		
		return null;
	}
	
}
